/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { beforeAll, describe, expect, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from './common/testCommon';
import path from 'path';
import { Rule } from '../../../src/Index';
import { CheckEntry } from '../../../src/utils/common/CheckEntry';
import { OnPageHideReleaseCheck } from '../../../src/checker/performance/OnPageHideReleaseCheck';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
    const rule: Rule = new Rule('');
    checkEntry = await testCaseCheck('./test/unittest/sample/OnPageHideRelease', rule, CHECK_MODE.PROJECT2CHECK, OnPageHideReleaseCheck);
    realPath = checkEntry.scene.getRealProjectDir();
});

describe('OnPageHideReleaseCheckTest', () => {
    test('OnPageHideReleaseCheckTest_001_Report', () => {
        const detectFile: string = path.join(realPath, 'ets', 'OnPageHideReleaseReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) =>
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(1);
        const report = detectFileReports[0];
        expect(report.defect.description).toContain('In the onPageHide method, resource release should be delayed');
        // 打印实际的行列号用于调试
        console.log(`Actual line: ${report.defect.reportLine}, column: ${report.defect.reportColumn}, fixKey: ${report.defect.fixKey}`);
        // 验证告警的行列号
        expect(report.defect.reportLine).toBe(14); // releaseCamera调用行
        expect(report.defect.reportColumn).toBe(10);
        expect(report.defect.fixKey).toBe('14%10%22%');
    });

    test('OnPageHideReleaseCheckTest_002_NoReport', () => {
        const detectFile: string = path.join(realPath, 'ets', 'OnPageHideReleaseNoReport.ets');
        let detectFileReports = checkEntry.projectCheck.issues.filter((issue) =>
            issue.defect.mergeKey.startsWith(detectFile)
        );
        expect(detectFileReports.length).toBe(0);
    });
});

describe('OnPageHideReleaseCheckComplexCases', () => {
    test('Complex_多个释放方法部分包裹', () => {
        const detectFile = path.join(realPath, 'ets', 'OnPageHideReleaseComplex.ets');
        let reports = checkEntry.projectCheck.issues.filter(issue => issue.defect.mergeKey.startsWith(detectFile));
        expect(reports.length).toBe(2); // releaseCamera, releaseOther
        // 打印实际的行列号用于调试
        reports.forEach((report, index) => {
            console.log(`Complex1 report ${index}: line ${report.defect.reportLine}, column ${report.defect.reportColumn}, fixKey ${report.defect.fixKey}`);
        });
        // 断言行列号和fixKey
        const expectedLines = [9, 11];
        const expectedColumns = [10, 10];
        const expectedFixKeys = ['9%10%22%', '11%10%21%'];
        reports.forEach((report, index) => {
            expect(report.defect.reportLine).toBe(expectedLines[index]);
            expect(report.defect.reportColumn).toBe(expectedColumns[index]);
            expect(report.defect.fixKey).toBe(expectedFixKeys[index]);
        });
    });
});
